﻿using GN.Pay.TradeChannel.Transfers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.WeiXin.Response
{
    /// <summary>
    /// 转账响应
    /// </summary>
    [Serializable]
    class TransfersResponse : AResponse
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="xml"></param>
        public TransfersResponse(string xml)
            : base(xml)
        {
        }

        /// <summary>
        /// 微信分配的公众账号ID（企业号corpid即为此appId）
        /// </summary>
        public string Mch_AppId { get; set; }

        /// <summary>
        /// 微信支付分配的商户号
        /// </summary>
        public string MchId { get; set; }

        /// <summary>
        /// 微信支付分配的终端设备号，
        /// </summary>
        public string Device_Info { get; set; }

        /// <summary>
        /// 商户订单号，需保持唯一性(只能是字母或者数字，不能包含有符号)
        /// </summary>
        public string Partner_Trade_No { get; set; }

        /// <summary>
        /// 企业付款成功，返回的微信订单号
        /// </summary>
        public string Payment_No { get; set; }

        /// <summary>
        /// 企业付款成功时间(yyyy-MM-dd HH:mm:ss)
        /// </summary>
        public string Payment_Time { get; set; }

        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public ITransfersApplyResult ToResult(ITradeChannel channel, ITransfersApplyInfo apply)
        {
            if (IsFullSuccess())
            {
                return ToResult(channel, apply, TradeApplyResultState.Complete_Success, "转账成功");
            }
            if ("NOTENOUGH".Equals(this.Err_Code, StringComparison.InvariantCultureIgnoreCase))
            {
                return ToResult(channel, apply, TradeApplyResultState.Balance_Insufficient_Error, "账户余额不足");
            }
            if ("OPENID_ERROR".Equals(this.Err_Code, StringComparison.InvariantCultureIgnoreCase))
            {
                return ToResult(channel, apply, TradeApplyResultState.Info_Error, "Openid格式错误或者不属于商家公众账号");
            }
            if ("NAME_MISMATCH".Equals(this.Err_Code, StringComparison.InvariantCultureIgnoreCase))
            {
                return ToResult(channel, apply, TradeApplyResultState.Info_Error, "姓名校验出错");
            }
            if ("V2_ACCOUNT_SIMPLE_BAN".Equals(this.Err_Code, StringComparison.InvariantCultureIgnoreCase))
            {
                return ToResult(channel, apply, TradeApplyResultState.Apply_Error, "无法给非实名用户付款");
            }
            if (!IsReturnSuccess())
            {
                return ToResult(channel, apply, TradeApplyResultState.Apply_Error, Return_Msg);
            }
            if (!IsResultSuccess())
            {
                if (string.IsNullOrWhiteSpace(Err_Code_Des))
                {
                    return ToResult(channel, apply, TradeApplyResultState.Apply_Error, Err_Code);
                }
                else
                {
                    return ToResult(channel, apply, TradeApplyResultState.Apply_Error, Err_Code_Des);
                }
            }
            throw channel.ThrowChannelValidationException("响应发生未知异常，退款申请结果不是已知的状态");
        }

        private ITransfersApplyResult ToResult(ITradeChannel channel, ITransfersApplyInfo apply, TradeApplyResultState state, string stateMsg)
        {
            DateTime? time = null;
            if (!string.IsNullOrWhiteSpace(Payment_Time))
            {
                time = DateTime.Parse(Payment_Time);
            }
            return new TransfersApplyResult(channel)
            {
                ResultState = state,
                ResultStateMessage = stateMsg,
                TradeNo = Payment_No,
                TransfersFee = 0m,
                TransfersMoney = apply.TransfersMoney,
                TransfersNo = Partner_Trade_No,
                TransfersTime = time
            };
        }
    }
}
